<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8"/>
    <link th:rel="stylesheet" th:href="@{/webjars/bootstrap/4.6.0-1/css/bootstrap.min.css} "/>
    <title>SMW</title>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark static-top">
    <div class="container">
        <a class="navbar-brand" href="/">Stock Market Watcher</a>
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarResponsive"
                aria-controls="navbarResponsive"
                aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="navbarResponsive">
            <ul class="navbar-nav ml-auto">
                <li class="nav-item">
                    <a class="nav-link" href="/">Home</a>
                </li>
                <li class="nav-item active">
                    <a class="nav-link" href="/subscriptions">Subscription
                        <span class="sr-only">(current)</span>
                    </a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#">Contact</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#" th:text="'Logged in as ' + ${email}"></a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" th:href="@{/logout}">Logout</a>
                </li>
            </ul>
        </div>
    </div>
</nav>
<div class="container">
    <div class="row">
        <div class="col-lg-10 mt-5 mb-5">
            <div th:if="${param.added}">
                <div class="alert alert-info" th:text="'成功订阅'+${param.added}">
                </div>
            </div>
            <h3>已订阅股票代码</h3>
            <ul class="list-group">
                <div th:each="subscription: ${subscriptions}">
                    <li class="list-group-item" th:text="${subscription.symbol}">
                    </li>
                </div>
            </ul>
        </div>
    </div>
</div>
<div class="container">
    <div class="row mt-5">
        <div class="col">
            <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#stockModal">
                新增订阅
            </button>
            <th:block th:replace="_stocksubscriptionmodal :: modal('stockModal')">modal</th:block>
        </div>
    </div>
</div>
<script th:src="@{/webjars/jquery/jquery.min.js}"></script>
<script th:src="@{/webjars/popper.js/umd/popper.min.js}"></script>
<script th:src="@{/webjars/bootstrap/js/bootstrap.min.js}"></script>

<script>
    $('#stockModal').on('show.bs.modal', function () {
        $.get("/stocks", function (data) {
            $('#stockModal').find('.modal-body').html(data);
        })
    });

    $("#stock_sub_form").on("submit", function(e) {
        var postData = $(this).serializeArray();
        var formURL = $(this).attr("action");
        $.ajax({
            url: formURL,
            type: "POST",
            data: postData,
            success: function(data, textStatus, jqXHR) {
                $('#stockModal .modal-header .modal-title').html("Result");
                $('#stockModal .modal-body').html(data);
                $("#submitForm").remove();
            },
            error: function(jqXHR, status, error) {
                console.log(status + ": " + error);
            }
        });
        e.preventDefault();
    });

    $("#submitForm").on('click', function() {
        $("#stock_sub_form").submit();
    });






</script>
</body>
</html>